import asyncio
import aiohttp
import aiofiles
import requests
from lxml import etree

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36"
}


async def pange(url):
    """获取第几个页面的所有图片链接"""
    async with aiohttp.ClientSession() as session:
        async with session.get(url=url, headers=headers) as resp:
            resp.encoding = "utf-8"
            content = await resp.text()
            tree = etree.HTML(content)
            hrefs = tree.xpath('//div[@class="TypeList"]/ul/li/a/@href')
            hrefs = ["https://www.umei.cc/" + i for i in hrefs]
            tasks = []
            for href in hrefs:
                # asyncio.run(asyncio.wait(img(href)))
                tasks.append(asyncio.create_task(img(href)))
            await asyncio.wait(tasks)


async def img(url):
    """获取图片链接"""
    async with aiohttp.ClientSession() as session:
        async with session.get(url=url, headers=headers) as resp:
            resp.encoding = "utf-8"
            content = await resp.text()
            tree = etree.HTML(content)
            href = tree.xpath('//p[@align="center"]/img/@src')[0]
            await asyncio.wait([asyncio.create_task(download(href))])


async def download(url):
    """保存图片"""
    print(url)
    async with aiohttp.ClientSession() as session:
        async with session.get(url=url, headers=headers) as resp:
            # aiohttp没有content
            # content = await resp.content
            # 字节形式
            content = await resp.read()
            # 获取图片名字
            name = url.split('/')[-1]
            async with aiofiles.open(f'./youmei/{name}', mode='wb') as fp:
                await fp.write(content)


async def main():
    """获取所有页数"""
    url = "https://www.umei.cc/katongdongman/index.htm"
    resp = requests.get(url=url, headers=headers)
    resp.encoding = "utf-8"
    tree = etree.HTML(resp.text)
    # 获取页面数
    pange_number = tree.xpath('//div[@class="NewPages"]/a/@href')[-1]
    pange_number = int(pange_number.split('_')[-1].split('.')[0])
    tasks = []
    # for cont in range(1, pange_number+1):
    for cont in range(1, 2):
        if cont == 1:
            # 因为第一页的链接不同
            tasks.append(asyncio.create_task(pange(url)))

        else:
            # 除去第一页的页面链接
            pange_url = f'https://www.umei.cc/katongdongman/index_{cont}.htm'
            tasks.append(asyncio.create_task(pange(pange_url)))
    await asyncio.wait(tasks)


def run():
    asyncio.run(main())


if __name__ == '__main__':
    run()
